package com.flower.common.search;

import org.apache.commons.lang3.StringUtils;

/**
 * 搜索工具类
 * 
 *
 */
public class SearchUtils {
	/**
	 * 过滤关键词
	 * @param keyWord
	 * @return
	 */
	public static String filterKeyWord(String keyWord) {
		/*
		 * 将关键词过滤掉标点符号,然后每个字符的间隔都拼接上%号
		 * 执行sql like模糊查询能匹配更多数据
		 */
		keyWord = keyWord.replaceAll("\\p{Punct}", "").trim();
		if (keyWord.length() < 20) {
			keyWord = keyWord.substring(0, keyWord.length());
		} else {
			keyWord = keyWord.substring(0, 20);
		}
		if(!StringUtils.isNotBlank(keyWord)) {
			return keyWord;
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < keyWord.length(); i++) {
			sb.append("%").append(keyWord.charAt(i));
		}
		sb.append("%");
		return sb.toString();
	}
	
	public static void main(String[] args) {
		String filterKeyWord = SearchUtils.filterKeyWord("abcsd");
		System.out.println(filterKeyWord);
	}

}
